package com.shuorikeji.huohuo.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.MhSdAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MhSdInfoBean;
import com.shuorikeji.huohuo.bean.MhSdMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.ui.SearchActivity;
import com.shuorikeji.huohuo.ui.sj.MhSdDetailActivity;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MhSdGcActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.ll_new_tab)
    LinearLayout llNewTab;
    @BindView(R.id.tv_new_tab)
    TextView tvNewTab;
    @BindView(R.id.iv_new_tab)
    ImageView ivNewTab;
    @BindView(R.id.ll_hot_tab)
    LinearLayout llHotTab;
    @BindView(R.id.tv_hot_tab)
    TextView tvHotTab;
    @BindView(R.id.iv_hot_tab)
    ImageView ivHotTab;
    @BindView(R.id.iv_search)
    ImageView ivSearch;

    private MhSdAdapter cjMhSdAdapter;
    private List<MhSdInfoBean> mhSdList = new ArrayList<>();
    private int curPage;
    private String isHot, isNews;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_mh_sd_gc);
    }

    @Override
    protected void initData() {
        initSmartRefreshLayout();
        initAdapter();
        setTab(1);
        curPage = 1;
        getSd();
        initClick();
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        cjMhSdAdapter = new MhSdAdapter(mContext, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhSdInfoBean data = (MhSdInfoBean) object;
                        Intent intent = new Intent(MhSdGcActivity.this, MhSdDetailActivity.class);
                        intent.putExtra("SD_ID", data.getId());
                        startActivity(intent);
                        break;
                    case 2:
                        //收藏
                        MhSdInfoBean scData = (MhSdInfoBean) object;
                        if (scData.getIs_collect() == 0) {
                            scMhSd(scData.getId());
                        } else {
                            qxScMhSd(scData.getId());
                        }
                        break;
                    case 3:
                        //点赞
                        MhSdInfoBean dzData = (MhSdInfoBean) object;
                        if (dzData.getIs_like() == 0) {
                            dzMhSd(dzData.getId());
                        } else {
                            qxDzMhSd(dzData.getId());
                        }
                        break;
                }
            }
        });
        rvData.setAdapter(cjMhSdAdapter);
    }

    private void initSmartRefreshLayout() {
        srlLayout.setRefreshHeader(new ClassicsHeader(MhSdGcActivity.this));
        srlLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //刷新
                curPage = 1;
                getSd();
            }
        });
        srlLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                //加载更多
                curPage = curPage + 1;
                getSd();
            }
        });
    }

    /**
     * @param flag 1最新
     *             2最热
     */
    private void setTab(int flag) {
        tvNewTab.setTextColor((flag == 1) ? getResources().getColor(R.color.color_333333) : getResources().getColor(R.color.color_999999));
        BusinessUtils.setTxtFold(tvNewTab, (flag == 1));
        ivNewTab.setVisibility((flag == 1) ? View.VISIBLE : View.INVISIBLE);

        tvHotTab.setTextColor((flag == 2) ? getResources().getColor(R.color.color_333333) : getResources().getColor(R.color.color_999999));
        BusinessUtils.setTxtFold(tvHotTab, (flag == 2));
        ivHotTab.setVisibility((flag == 2) ? View.VISIBLE : View.INVISIBLE);

        isHot = (flag == 1) ? "" : "2";
        isNews = (flag == 1) ? "2" : "";
    }

    private void getSd() {
        mPresenter.mhSdGc(true, 20, curPage, isHot, isNews, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                srlLayout.finishLoadMore();
                srlLayout.finishRefresh();
                switch (flag) {
                    case 1:
                        MhSdMainBean data = (MhSdMainBean) object;
                        if (curPage == 1) {
                            mhSdList.clear();
                        }
                        mhSdList.addAll(data.getData().getData());
                        cjMhSdAdapter.setData(mhSdList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhSdGcActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void scMhSd(int id) {
        mPresenter.scMhSd(true, id, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        for (int i = 0; i < mhSdList.size(); i++) {
                            if (id == mhSdList.get(i).getId()) {
                                mhSdList.get(i).setCollect_num(mhSdList.get(i).getCollect_num() + 1);
                                mhSdList.get(i).setIs_collect(1);
                                break;
                            }
                        }
                        cjMhSdAdapter.setData(mhSdList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhSdGcActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void qxScMhSd(int id) {
        mPresenter.qxScMhSd(true, id, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        for (int i = 0; i < mhSdList.size(); i++) {
                            if (id == mhSdList.get(i).getId()) {
                                mhSdList.get(i).setCollect_num(mhSdList.get(i).getCollect_num() - 1);
                                mhSdList.get(i).setIs_collect(0);
                                break;
                            }
                        }
                        cjMhSdAdapter.setData(mhSdList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhSdGcActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void dzMhSd(int id) {
        mPresenter.dzMhSd(true, id, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        for (int i = 0; i < mhSdList.size(); i++) {
                            if (id == mhSdList.get(i).getId()) {
                                mhSdList.get(i).setLike_num(mhSdList.get(i).getLike_num() + 1);
                                mhSdList.get(i).setIs_like(1);
                                break;
                            }
                        }
                        cjMhSdAdapter.setData(mhSdList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhSdGcActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void qxDzMhSd(int id) {
        mPresenter.qxDzMhSd(true, id, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        for (int i = 0; i < mhSdList.size(); i++) {
                            if (id == mhSdList.get(i).getId()) {
                                mhSdList.get(i).setLike_num(mhSdList.get(i).getLike_num() - 1);
                                mhSdList.get(i).setIs_like(0);
                                break;
                            }
                        }
                        cjMhSdAdapter.setData(mhSdList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhSdGcActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        llNewTab.setOnClickListener(view -> {
            setTab(1);
            curPage = 1;
            getSd();
        });
        llHotTab.setOnClickListener(view -> {
            setTab(2);
            curPage = 1;
            getSd();
        });
        ivSearch.setOnClickListener(view -> {
            Intent intent = new Intent(MhSdGcActivity.this, SearchActivity.class);
            startActivity(intent);
        });
    }
}